package com.shanghaionstar.home.function;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.onstar.cn.auth.userProfile.Vehicle;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.function.FunctionInfo;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.DiscoverActivity;
import com.shanghaionstar.activity.MoreActivity;
import com.shanghaionstar.activity.webview.handler.CommonWebViewActivity;
import com.shanghaionstar.dealersorder.DealersOrderActivity;
import com.shanghaionstar.discover.OVDEmailFragment;
import com.shanghaionstar.discover.WifiHomeFragment;
import com.shanghaionstar.discover.charge.ChargeModeFragment;
import com.shanghaionstar.discover.dealer.PreferDealerFragment;
import com.shanghaionstar.discover.geofence.GeofenceHomeActivity;
import com.shanghaionstar.discover.violation.VehicleViolationActivity;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.nav.NavActivity;
import com.shanghaionstar.nav.NavMapFragment;
import com.shanghaionstar.servicepackage.OnstarBuyPackageActivity;
import com.shanghaionstar.smart.SmartHomeActivity;
import com.shanghaionstar.user.RemoteControlHistoryFragment;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.JoinMeConstants;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Rengui Lu on 2016/8/2.
 */

public class ShortCutManager {
    private Context context;
//    private ShortCutManager instance;
    private static final String NO_LOGIN = "noLogin";
    private static final String VIN_FLAG = "_function";

    public ShortCutManager(Context context) {
        this.context = context;
    }

//    public ShortCutManager getInstance(Context context) {
//        instance = new ShortCutManager(context);
//        return instance;
//    }


////    <item>升级为车主</item>
////        <item>联系安吉星</item>
////        <item>真实故事</item>
////        <item>服务介绍</item>
//
//    FunctionNameFindId[] personalFindIds = {FunctionNameFindId.UnSend,FunctionNameFindId.Quickly_TrueStory,
//            FunctionNameFindId.Quickly_Introduction};
//    String[] personalPages = {OVDEmailFragment.class.getName(),
//            OVDEmailFragment.class.getName(), OVDEmailFragment.class.getName()};
//    Integer[] personalIconIds = { R.drawable.sc_co_us,
//            R.drawable.sc_r_s, R.drawable.sc_s_in};
//    String[] personalActivitys = {MoreActivity.class.getName(), MoreActivity.class.getName(), MoreActivity.class.getName(),
//            MoreActivity.class.getName()};

//        <item>检测报告</item>
//        <item>经销商预约</item>
//        <item>联系安吉星</item>
//        <item>违章查询</item>
//        <item>智能家居</item>

//    <string-array name="vehicle_sevices_list">
//    <item>车辆位置</item>
//    <item>预约经销商</item>
//    <item>首选经销商</item>
//    <item>车辆操作历史</item>
//    <item>电子围栏</item>
//    <item>智能家居</item>
//    <item>充电管理</item>
//    <item>车载Wi-Fi</item>
//    <item>违章查询</item>
//    <item>检测报告</item>
//    <item>联系安吉星</item>
//    <item>车主俱乐部</item>
//    <item>购买套餐包</item>
//    </string-array>

    FunctionNameFindId[] vehicleServicePageIds = {FunctionNameFindId.Quickly_locator, FunctionNameFindId.Quickly_DealerReservation,
            FunctionNameFindId.Quickly_PreferDealer
            , FunctionNameFindId.Quickly_RecentHistory, FunctionNameFindId.Quickly_SecurityZone,
            FunctionNameFindId.Quickly_SmartHome, FunctionNameFindId.Quickly_ChargeManagement
            , FunctionNameFindId.Quickly_VehicularWiFi, FunctionNameFindId.Quickly_Violationcheck
            , FunctionNameFindId.Quickly_OVD, FunctionNameFindId.UnSend, FunctionNameFindId.Quickly_Club, FunctionNameFindId.Quick_ServicePackage};

    String[] vehicleServicePages = {OVDEmailFragment.class.getName(), DealersOrderActivity.class.getName(),
            PreferDealerFragment.class.getName(), RemoteControlHistoryFragment.class.getName(),
            GeofenceHomeActivity.class.getName(), SmartHomeActivity.class.getName(),
            ChargeModeFragment.class.getName(), WifiHomeFragment.class.getName(),
            VehicleViolationActivity.class.getName(), OVDEmailFragment.class.getName(),
            OVDEmailFragment.class.getName(), CommonWebViewActivity.class.getName()
            ,OnstarBuyPackageActivity.class.getName(),};

    Integer[] vehicleServiceIconIds = {R.drawable.sc_v_p, R.drawable.sc_de_or, R.drawable.sc_pr_de, R.drawable.sc_v_op,
            R.drawable.sc_ge_fe, R.drawable.sc_sm_h, R.drawable.sc_ch_mo, R.drawable.sc_wi_ma,
            R.drawable.sc_q_il, R.drawable.sc_v_re, R.drawable.sc_co_us, R.drawable.sc_br_cl,R.drawable.sc_p_p};

    String[] vehicleServiceActivitys = {NavActivity.class.getName(), DealersOrderActivity.class.getName(),
            DiscoverActivity.class.getName(), MoreActivity.class.getName(),
            GeofenceHomeActivity.class.getName(), SmartHomeActivity.class.getName(),
            DiscoverActivity.class.getName(), DiscoverActivity.class.getName(),
            VehicleViolationActivity.class.getName(), DiscoverActivity.class.getName(),
            DiscoverActivity.class.getName(), CommonWebViewActivity.class.getName(),
            OnstarBuyPackageActivity.class.getName()
    };

//    <string-array name="vehicle_control_list">
//        <item>首选经销商</item>
//        <item>车辆操作历史</item>
//        <item>车载Wi-Fi</item>
//        <item>电子围栏</item>
//        <item>充电模式</item>
//        </string-array>

//    FunctionNameFindId[] vehicleControlPageIds = {FunctionNameFindId.Quickly_PreferDealer, FunctionNameFindId.Quickly_RecentHistory
//            , FunctionNameFindId.Quickly_VehicularWiFi
//            , FunctionNameFindId.Quickly_SecurityZone
//            , FunctionNameFindId.Quickly_ChargeManagement};
//    String[] vehicleControlPages = {PreferDealerFragment.class.getName(), OVDEmailFragment.class.getName(),
//            WifiHomeFragment.class.getName(), GeofenceHomeActivity.class.getName(), ChargeModeFragment.class.getName()};
//    Integer[] vehicleControlIconIds = {R.drawable.sc_de_or, R.drawable.sc_v_op,
//            R.drawable.sc_wi_ma, R.drawable.sc_ge_fe, R.drawable.sc_ch_mo};
//    String[] vehicleControlActivitys = {DiscoverActivity.class.getName(), DiscoverActivity.class.getName(), DiscoverActivity.class.getName()
//            , GeofenceHomeActivity.class.getName(), DiscoverActivity.class.getName()};

//    <string-array name="poi_list">
//    <item>实时路况</item>
//    <item>兴趣点搜索</item>
//    <item>一键回家</item>
//    <item>一键回公司</item>
//    </string-array>

    FunctionNameFindId[] poiPageIds = {FunctionNameFindId.Quickly_ShowTraffic
            , FunctionNameFindId.Quickly_SearchPOI
            , FunctionNameFindId.Quickly_ToHome
            , FunctionNameFindId.Quickly_ToOffice};
    String[] poiPages = {NavMapFragment.class.getName(),
            NavMapFragment.class.getName(), NavMapFragment.class.getName(), NavMapFragment.class.getName()};
    Integer[] poiIconIds = {R.drawable.sc_cu_tr,
            R.drawable.sc_se_in, R.drawable.sc_go_h, R.drawable.sc_go_c};
    String[] poiActivitys = {NavActivity.class.getName(), NavActivity.class.getName(), NavActivity.class.getName()
            , NavActivity.class.getName(), NavActivity.class.getName()};

//    <string-array name="remote_control_list">
//    <item>远程启动</item>
//    <item>取消启动</item>
//    <item>车门上锁</item>
//    <item>车门解锁</item>
//    <item>闪灯鸣笛</item>
//    </string-array>

    FunctionNameFindId[] remoteControlIds = {FunctionNameFindId.Quickly_RemoteStart, FunctionNameFindId.Quickly_CancelStart
            , FunctionNameFindId.Quickly_DoorLock
            , FunctionNameFindId.Quickly_DoorUnlock
            , FunctionNameFindId.Quickly_HornsLights};
    String[] remoteControlPages = {OVDEmailFragment.class.getName(), OVDEmailFragment.class.getName(),
            OVDEmailFragment.class.getName(), OVDEmailFragment.class.getName(), OVDEmailFragment.class.getName()};
    Integer[] remoteControlIconIds = {R.drawable.sc_re_op, R.drawable.sc_c_p,
            R.drawable.sc_v_l, R.drawable.sc_un_lock, R.drawable.sc_fl_sw};
    String[] remoteControlActivitys = {MoreActivity.class.getName(), MoreActivity.class.getName(), MoreActivity.class.getName()
            , MoreActivity.class.getName(), MoreActivity.class.getName()};

//    public interface IFunRole {
//        String VISITOR = "Visitor";
//        String ANONYMOUS_OR_GEN9_OWENER = "Anonymous/Gen9 owner";
//        String GEN9_DRIVER_OR_PROXY = "Gen9 driver/proxy";
//        String GEN10_OWNER = "Gen10 owner";
//        String GEN10_DRIVER_OR_PROXY = "Gen10 driver/proxy";
//        String PHEV_OWNER = "PHEV owner";
//        String PHEV_DRIVER_OR_PROXY = "PHEV driver/proxy";
//    }

    private List<FunctionInfo> list = new ArrayList<>();

    private void setupNor(String[] titles, String[] pages, Integer[] icons, String[] activitys, FunctionNameFindId[] ids, String type, int... total) {
        int N = (total != null && total.length > 0 && total[0] > 0) ? total[0] : titles.length;
        for (int i = 0; i < N; i++) {
            FunctionInfo shortCuts = new FunctionInfo();
            shortCuts.setIconResId(icons[i]);
            shortCuts.setTitle(titles[i]);
            shortCuts.setFragment(pages[i]);
            shortCuts.setActivity(activitys[i]);
            shortCuts.setFunctionOrder(i);
            shortCuts.setFunctionNameFindId(ids[i].getStringValue());
            shortCuts.setFunctionType(type);
            list.add(shortCuts);
        }
    }

//    private void setupNorProxy(String[] titles, String[] pages, Integer[] icons, String[] activitys, FunctionNameFindId[] ids, String type, int[] sels) {
//        int N = titles.length;
//        for (int i = 0; i < N; i++) {
//            for (int s : sels) {
//                if (i == s) {
//                    FunctionInfo shortCuts = new FunctionInfo();
//                    shortCuts.setIconResId(icons[i]);
//                    shortCuts.setTitle(titles[i]);
//                    shortCuts.setFragment(pages[i]);
//                    shortCuts.setActivity(activitys[i]);
//                    shortCuts.setFunctionOrder(i);
//                    shortCuts.setFunctionNameFindId(ids[i].getStringValue());
//                    shortCuts.setFunctionType(type);
//                    list.add(shortCuts);
//                }
//            }
//        }
//    }

//    private void setupOwner(String role) {
//        String[] vehicleServicesStrList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
//        if (role.equals(IFunRole.GEN10_OWNER)) {
//            setupNor(vehicleServicesStrList, vehicleServicePages, vehicleServiceIconIds, vehicleServiceActivitys, vehicleServicePageIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.VehicleState));
//        } else {
//            setupNor(vehicleServicesStrList, vehicleServicePages, vehicleServiceIconIds, vehicleServiceActivitys, vehicleServicePageIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.VehicleState), 4);
//        }
//        String[] poiStrList = context.getResources().getStringArray(R.array.poi_list);
//        setupNor(poiStrList, poiPages, poiIconIds, poiActivitys, poiPageIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.Navigation));
//
//    }
//
//    private void setupProxy(String role) {
//        String[] vehicleServicesStrList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
//        setupNorProxy(vehicleServicesStrList, vehicleServicePages, vehicleServiceIconIds, vehicleServiceActivitys, vehicleServicePageIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.VehicleState), new int[]{0, 2, 3});
//        String[] vehicleControlStrList = context.getResources().getStringArray(R.array.vehicle_control_list);
//        if (role.equals(IFunRole.GEN9_DRIVER_OR_PROXY)) {
//            setupNorProxy(vehicleControlStrList, vehicleControlPages, vehicleControlIconIds, vehicleControlActivitys, vehicleControlPageIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.VehicleControl), new int[]{0});
//        } else {
//            setupNorProxy(vehicleControlStrList, vehicleControlPages, vehicleControlIconIds, vehicleControlActivitys, vehicleControlPageIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.VehicleControl), new int[]{0, 2});
//        }
//        String[] poiStrList = context.getResources().getStringArray(R.array.poi_list);
//        setupNorProxy(poiStrList, poiPages, poiIconIds, poiActivitys, poiPageIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.Navigation), new int[]{1, 2, 3, 4});
//    }
//
//    private void setupVisitor() {
//        String[] personalStrList = context.getResources().getStringArray(R.array.personal_sevices_list);
//        setupNor(personalStrList, personalPages, personalIconIds, personalActivitys, personalFindIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.VistorServices));
//       olActivitys, remoteControlIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.RemoteControl));
//    }

    public void setupAll() {
        setupProxyAll();
        String[] remoteControlStrList = context.getResources().getStringArray(R.array.remote_control_list);
        setupNor(remoteControlStrList, remoteControlPages, remoteControlIconIds, remoteControlActivitys, remoteControlIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.RemoteControl));
    }

    public void setupProxyAll() {
        String[] vehicleServicesStrList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
        setupNor(vehicleServicesStrList, vehicleServicePages, vehicleServiceIconIds, vehicleServiceActivitys, vehicleServicePageIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.VehicleState));
        String[] poiStrList = context.getResources().getStringArray(R.array.poi_list);
        setupNor(poiStrList, poiPages, poiIconIds, poiActivitys, poiPageIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.Navigation));
    }

    public void removeItems(List<FunctionInfo> list, String... items) {
        if (items == null)
            return;
        Iterator<FunctionInfo> it = list.iterator();
        while (it.hasNext()) {
            FunctionInfo info = it.next();
            for (String item : items) {
                if (item.equalsIgnoreCase(info.getTitle())) {
                    it.remove();
                    break;
                }
            }
        }
    }

//    <string-array name="vehicle_sevices_list">
//    <item>车辆位置</item>
//    <item>预约经销商</item>
//    <item>首选经销商</item>
//    <item>车辆操作历史</item>
//    <item>电子围栏</item>
//    <item>智能家居</item>
//    <item>充电管理</item>
//    <item>车载Wi-Fi</item>
//    <item>违章查询</item>
//    <item>检测报告</item>
//    <item>联系安吉星</item>
//    </string-array>

    public List<FunctionInfo> getData() {
        getDataContent();
//        removeSmartHome(list);
//        removeVehicleViolation(list);
        cleanData(list);
        return list;
    }

    /**
     * 移除违章查询
     *
     * @param list
     */
    private void removeVehicleViolation(List<FunctionInfo> list) {
        Iterator<FunctionInfo> it = list.iterator();
        while (it.hasNext()) {
            FunctionInfo info = it.next();
            if (info.getActivity().equalsIgnoreCase(VehicleViolationActivity.class.getName())) {
                it.remove();
                break;
            }
        }
    }

    private void cleanPurchaseOnstarPackage(List<FunctionInfo> list){
        boolean isLogin = ApplicationUtil.getInstance().isLogin();
        String loginRole = ApplicationUtil.getInstance().getLoginRole();
        boolean isNull = CommonUtil.isNull(loginRole);
        boolean isVisitor = !isNull & loginRole.equalsIgnoreCase(JoinMeConstants.VISITOR);
        boolean isOwner = loginRole.equalsIgnoreCase(JoinMeConstants.OWNER);
        if(isLogin && !isVisitor && !isOwner){
            String[] titleList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
            removeItem(list,titleList[12]);
        }
    }
    private void cleanChevrolet(List<FunctionInfo> list){
        boolean isChevrolet = isChevrolet();
        if(isChevrolet) {
            String[] titleList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
            String title = titleList[11];
            Iterator<FunctionInfo> it = list.iterator();
            while (it.hasNext()) {
                FunctionInfo info = it.next();
                if (info.getTitle().equalsIgnoreCase(title)) {
                    if (isChevrolet) {
                        info.setIconResId(R.drawable.sc_c_club);
                        info.setFunctionNameFindId(FunctionNameFindId.Quick_Club.getStringValue());
                    }
                    break;
                }
            }
        }

    }

    private void cleanData(List<FunctionInfo> list){
        removeVehicleViolation(list);
        cleanPurchaseOnstarPackage(list);
        cleanChevrolet(list);
    }

    private void removeItem(List<FunctionInfo> list,String title) {
        Iterator<FunctionInfo> it = list.iterator();
        while (it.hasNext()) {
            FunctionInfo info = it.next();
            if (info.getTitle().equalsIgnoreCase(title)) {
                it.remove();
                break;
            }
        }
    }

    private void removeSmartHome(List<FunctionInfo> list) {
        Iterator<FunctionInfo> it = list.iterator();
        while (it.hasNext()) {
            FunctionInfo info = it.next();
            if (info.getActivity().equalsIgnoreCase(SmartHomeActivity.class.getName())) {
                it.remove();
                break;
            }
        }
    }

    public boolean isAvailableBrand(){
        return  isCadillac() || isChevrolet();
    }
    
    public boolean isCadillac() {
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        if(vehicle == null)
            return false;
        String desc = vehicle.getModelDesc();
        return  desc.contains("凯迪拉克") || desc.toLowerCase().contains("Cadillac".toLowerCase());
    }

    public boolean isChevrolet() {
//        (desc.contains("雪佛兰") || desc.toLowerCase().contains("Chevrolet".toLowerCase()))
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        if(vehicle == null)
            return false;
        String desc = vehicle.getModelDesc();
        return desc.contains("雪佛兰") || desc.toLowerCase().contains("Chevrolet".toLowerCase());
    }


    public List<FunctionInfo> getDataContent() {
        list.clear();
        boolean isLogin = ApplicationUtil.getInstance().isLogin();
        if (!isLogin)
            return getVisitorOrAnonymous();
        String loginRole = ApplicationUtil.getInstance().getLoginRole();
        boolean isNull = CommonUtil.isNull(loginRole);
        boolean isVisitor = !isNull & loginRole.equalsIgnoreCase(JoinMeConstants.VISITOR);
        if (isVisitor)
            return getVisitorOrAnonymous();
        boolean isOwner = loginRole.equalsIgnoreCase(JoinMeConstants.OWNER);
        boolean isGen10 = ApplicationUtil.getInstance().isGen10();
        boolean isPhev = ApplicationUtil.getInstance().isPhev();

        //GEN10 车主 PHEV
        if (isGen10 && isOwner && isPhev)
            return getGen10OwnerEv();
        //GEN10 车主 非Phev
        if (isGen10 && isOwner && !isPhev)
            return getGen10OwnerNoEv();
        //GEN10 非车主 Phev
        if (isGen10 && !isOwner && isPhev)
            return getGen10PorxyEv();
        if (isGen10 && !isOwner && !isPhev)
            return getGen10PorxyNoEv();
        if (!isGen10 && isOwner && isPhev)
            return getGen9OwnerEv();
        if (!isGen10 && isOwner && !isPhev)
            return getGen9OwnerNoEv();
        if (!isGen10 && !isOwner && isPhev)
            return getGen9ProxyEv();
        if (!isGen10 && !isOwner && !isPhev)
            return getGen9ProxyNoEv();
        return null;
    }

    private List<FunctionInfo> getGen9OwnerEv() {
        String[] titleList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
        setupAll();
        boolean isAvailableBrand = isAvailableBrand();
        if (isAvailableBrand) {
            removeItems(list, titleList[4], titleList[5], titleList[7]);
        } else {
            removeItems(list, titleList[4], titleList[5], titleList[7], titleList[11]);
        }
        return list;
    }

//    private void handleCadillac(List<FunctionInfo> list,String[] titleList){
//        boolean isAvailableBrand = isAvailableBrand();
//        if(isAvailableBrand) {
//            removeItems(list, titleList[4], titleList[5], titleList[7]);
//        }else{
//            removeItems(list, titleList[4], titleList[5], titleList[7], titleList[11]);
//        }
//    }

    private List<FunctionInfo> getGen9OwnerNoEv() {
        String[] titleList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
        setupAll();
        boolean isAvailableBrand = isAvailableBrand();
        if (isAvailableBrand) {
            removeItems(list, titleList[4], titleList[5], titleList[6], titleList[7]);
        } else {
            removeItems(list, titleList[4], titleList[5], titleList[6], titleList[7], titleList[11]);
        }
        return list;
    }

    private List<FunctionInfo> getGen10OwnerEv() {
        String[] titleList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
        setupAll();
        boolean isAvailableBrand = isAvailableBrand();
        if (!isAvailableBrand) {
            removeItems(list, titleList[11]);
        }
        return list;
    }

    private List<FunctionInfo> getVisitorOrAnonymous() {
        setupAll();
        return list;
    }

    private List<FunctionInfo> getGen10OwnerNoEv() {
        String[] titleList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
        setupAll();
        boolean isAvailableBrand = isAvailableBrand();
        if (isAvailableBrand) {
            removeItems(list, titleList[6]);
        } else {
            removeItems(list, titleList[6], titleList[11]);
        }
        return list;
    }

    private List<FunctionInfo> getGen10PorxyNoEv() {
        String[] titleList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
        setupProxyAll();
        removeItems(list, titleList[3], titleList[4], titleList[5], titleList[6],titleList[8], titleList[11], titleList[12]);
        return list;
    }

    private List<FunctionInfo> getGen10PorxyEv() {
        String[] titleList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
        setupProxyAll();
        removeItems(list, titleList[3], titleList[4], titleList[5], titleList[8],titleList[11], titleList[12]);
        return list;
    }

    private List<FunctionInfo> getGen9ProxyEv() {
        String[] titleList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
        setupProxyAll();
        removeItems(list, titleList[3], titleList[4], titleList[5], titleList[7], titleList[8], titleList[11],titleList[12]);
        return list;
    }

    private List<FunctionInfo> getGen9ProxyNoEv() {
        String[] titleList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
        setupProxyAll();
        removeItems(list, titleList[3], titleList[4], titleList[5], titleList[6], titleList[7], titleList[8], titleList[11],titleList[12]);
        return list;
    }

//    private void setupGen10OwnerPhev(){
//        String[] vehicleServicesStrList = context.getResources().getStringArray(R.array.vehicle_sevices_list);
//        if (role.equals(IFunRole.GEN10_OWNER)) {
//            setupNor(vehicleServicesStrList, vehicleServicePages, vehicleServiceIconIds, vehicleServiceActivitys, vehicleServicePageIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.VehicleState));
//        } else {
//            setupNor(vehicleServicesStrList, vehicleServicePages, vehicleServiceIconIds, vehicleServiceActivitys, vehicleServicePageIds, FunctionInfo.getFunctionTypeDesc(context, FunctionInfo.VehicleState), 4);
//        }
//
//    }

//    //获取数据
//    public List<FunctionInfo> getData() {
//        boolean isLogin = ApplicationUtil.getInstance().isLogin();
//        String loginRole = ApplicationUtil.getInstance().getLoginRole();
//        boolean isNull = CommonUtil.isNull(loginRole);
//        boolean isVisitor = !isNull & loginRole.equalsIgnoreCase(JoinMeConstants.VISITOR);\
//        //未登录和visitor
//        if (!isLogin || isVisitor) {
//            String role = IFunRole.ANONYMOUS_OR_GEN9_OWENER;
//            setupOwner(role);
//            return list;
//        }
//
//        boolean isGen10 = ApplicationUtil.getInstance().getCurrentVehicle();
//        boolean isOwner = loginRole.equalsIgnoreCase(JoinMeConstants.OWNER);
//        if (!isNull & !isGen10 & isOwner) {
//            setupOwner(IFunRole.ANONYMOUS_OR_GEN9_OWENER);
//            return list;
//        }
//        if (!isNull & !isGen10 & !isOwner) {
//            setupProxy(IFunRole.GEN9_DRIVER_OR_PROXY);
//            return list;
//        }
//        if (!isNull & isGen10 & isOwner) {
//            setupOwner(IFunRole.GEN10_OWNER);
//            return list;
//        }
//        if (!isNull & isGen10 & !isOwner) {
//            setupProxy(IFunRole.GEN10_DRIVER_OR_PROXY);
//            return list;
//        }
//        boolean isPhev = ApplicationUtil.getInstance().isPhev();
//        if (!isNull & isGen10 & isPhev & isOwner) {
//            setupOwner(IFunRole.PHEV_OWNER);
//            return list;
//        }
//        if (!isNull & isGen10 & isPhev & !isOwner) {
//            setupOwner(IFunRole.PHEV_DRIVER_OR_PROXY);
//            return list;
//        }
//        return list;
//    }

    private String getVin(Context context) {
        boolean isLogin = ApplicationUtil.getInstance().isLogin();
        if (!isLogin)
            return NO_LOGIN;
        String loginRole = ApplicationUtil.getInstance().getLoginRole();
        boolean isNull = CommonUtil.isNull(loginRole);
        boolean isVisitor = !isNull & loginRole.equalsIgnoreCase(JoinMeConstants.VISITOR);
        if (isVisitor)
            return ApplicationUtil.getInstance().getIdpID();
        return ApplicationUtil.getInstance().getVin()+"_"+ApplicationUtil.getInstance().getAccountId();
    }

    public void updateAll(Collection<FunctionInfo> infos) {
        String vin = getVin(context);
        vin += VIN_FLAG;
        PreferencesUtls.getInstance(context).putString(vin, new Gson().toJson(infos));
    }

    public List<FunctionInfo> getShortCuts() {
        String vin = getVin(context);
        return getShortCuts(vin);
    }

    /**
     * @param vin
     * @return 从文件中获取上次操作的数据
     */
    public List<FunctionInfo> getShortCuts(String vin) {
        List<FunctionInfo> tempList = new ArrayList<>();
        String funInfos = PreferencesUtls.getInstance(context).getString(vin + VIN_FLAG, "");
        if (TextUtils.isEmpty(funInfos)) {
            tempList = defaultShortCuts();
        } else {
            tempList = new Gson().fromJson(funInfos, new TypeToken<List<FunctionInfo>>() {
            }.getType());
            //预防出错后，重新调整
            if (tempList == null || tempList.size() < 4 || tempList.size() > 8) {
                PreferencesUtls.getInstance(context).putString(vin + VIN_FLAG, "");
                defaultShortCuts();
            } else {
                tempList = getRealShortCuts(tempList);
            }
        }
        addMore(tempList);
        //移除违章
        removeVehicleViolation(tempList);
        return tempList;
    }

    private List<FunctionInfo> getRealShortCuts(List<FunctionInfo> tempList) {
        List<FunctionInfo> list = getData();
        List<FunctionInfo> mList = new ArrayList<>();
//        list.retainAll(tempList);
        for (FunctionInfo info : tempList) {
            for (FunctionInfo entity : list) {
                if (entity.equals(info)) {
                    entity.setInHotseat(true);
                    entity.setOrderInHotseat(info.getOrderInHotseat());
                    mList.add(entity);
                }
            }
        }
        return mList;
    }

    /**
     * @param tempList 添加更多项
     */
    private void addMore(List<FunctionInfo> tempList) {
        FunctionInfo shortCuts = new FunctionInfo();
        shortCuts.setIconResId(R.drawable.sc_m_s);
        shortCuts.setTitle(context.getString(R.string.nav_more_search));
        shortCuts.setActivity(EditShortCutActivity.class.getName());
        if (!tempList.isEmpty()) {
            boolean containMore = false;
            for (FunctionInfo info : tempList) {
                if (info.getTitle().equals(shortCuts.getTitle())) {
                    containMore = true;
                    break;
                }
            }
            if (!containMore)
                tempList.add(shortCuts);
        }
    }

    private List<FunctionInfo> getRealShortCut(List<FunctionInfo> argList) {
        List<FunctionInfo> tempList = new ArrayList<FunctionInfo>();
        for (FunctionInfo entity : list) {
            for (FunctionInfo info : argList) {
                if (info.equals(entity)) {
                    entity.setOrderInHotseat(info.getOrderInHotseat());
                    entity.setInHotseat(true);
                    tempList.add(entity);
                }
            }
        }
        return tempList;
    }


    private void setupNormal(String[] defaultItems, List<FunctionInfo> list, List<FunctionInfo> tempList, String vin) {
        int gen9OwnerN = defaultItems.length;
        for (int j = 0; j < gen9OwnerN; j++) {
            for (FunctionInfo entity : list) {
                if (defaultItems[j].equalsIgnoreCase(entity.getTitle())) {
                    entity.setVin(vin);
                    entity.setOrderInHotseat(j);
                    tempList.add(entity);
                    break;
                }
            }
        }
    }

    /**
     * @return 本地没有记录
     * 根据用户状态获取默认数据
     */
    private List<FunctionInfo> defaultShortCuts() {
        List<FunctionInfo> list = getData();
        List<FunctionInfo> tempList = new ArrayList<>();
        boolean isLogin = ApplicationUtil.getInstance().isLogin();
        //未登录
        if (!isLogin) {
            String[] defaultGen9OwnerItems = context.getResources().getStringArray(R.array.default_visitor_anonymous_shortcut);
            setupNormal(defaultGen9OwnerItems, list, tempList, NO_LOGIN);
            return tempList;
        }

        String loginRole = ApplicationUtil.getInstance().getLoginRole();
        boolean isNull = CommonUtil.isNull(loginRole);
        boolean isVisitor = !isNull & loginRole.equalsIgnoreCase(JoinMeConstants.VISITOR);
        //访客
        if (isVisitor) {
            String ipdId = ApplicationUtil.getInstance().getIdpID();
            String[] defaultVisitorItems = context.getResources().getStringArray(R.array.default_visitor_anonymous_shortcut);
            setupNormal(defaultVisitorItems, list, tempList, ipdId);
            return tempList;
        }
//        boolean isGen10 = ApplicationUtil.getInstance().getCurrentVehicle();
        boolean isGen10 = ApplicationUtil.getInstance().isGen10();
        boolean isOwner = loginRole.equalsIgnoreCase(JoinMeConstants.OWNER);
        boolean isPhev = ApplicationUtil.getInstance().isPhev();
        boolean isAvailableBrand = isAvailableBrand();


        //gen9 owner
        if (isLogin & isOwner & isAvailableBrand) {
            String vin = ApplicationUtil.getInstance().getVin();
//            String[] defaultGen9OwnerItems = isAvailableBrand?context.getResources().getStringArray(R.array.default_brand_gen9_shortcut)
//                    :context.getResources().getStringArray(R.array.default_gen9_shortcut);
            String[] defaultGen9OwnerItems = context.getResources().getStringArray(R.array.default_brand_owner_shortcut);
            setupNormal(defaultGen9OwnerItems, list, tempList, vin);
            return tempList;
        }
        //gen10 owner
        if (isLogin  & isOwner & !isAvailableBrand) {
            String vin = ApplicationUtil.getInstance().getVin();
            String[] defaultGen10OwnerItems = isGen10?context.getResources().getStringArray(R.array.default_buick_gen10_shortcut)
                    :context.getResources().getStringArray(R.array.default_buick_gen9_shortcut);
            setupNormal(defaultGen10OwnerItems, list, tempList, vin);
            return tempList;
        }
        //proxy or driver
        if ((isLogin & !isOwner)) {
            String vin = ApplicationUtil.getInstance().getVin();
            String[] defaultProxyItems = context.getResources().getStringArray(R.array.default_proxy_shortcut);
            setupNormal(defaultProxyItems, list, tempList, vin);
            return tempList;
        }
        return tempList;
    }


}
